package cn.tedu.thread;

import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

/**本类用于多线程编程实现方案二*/
public class TestRunnable {
    public static void main(String[] args) {
        MyRunnable target=new MyRunnable();
        //把接口实现类对象与Thread类建立关系
//        Thread t1=new Thread(target,"赵彪");
//        Thread t2=new Thread(target,"马钊");
//        Thread t3=new Thread(target);
//        Thread t4=new Thread(target);
////        target.run();--
//        t1.start();
//        t2.start();
//        t3.start();
//        t4.start();

        //创建线程池
        /**创建线程池的工具:Executors使用
         * newFixedThreadPool(线程数)方法来创建出指定线程数的线程池
         * 创建出来的线程池类型是:ExecutorService*/
        ExecutorService pool = Executors.newFixedThreadPool(5);
        for (int i = 0; i < 5; i++) {
            //让池对象来完成任务,任务就是参数target
            pool.execute(target);
        }

    }
}
/**方式二:implements Runnable*/
class MyRunnable implements Runnable{
    //2.把业务放入run(),实现的是Runnable的抽象方法
    //Thread.currentThread()
    //获取当前正在执行的线程对象,静态方法可以被Thread类名直接调用
    public void run() {

            System.out.println(Thread.currentThread().getName());

    }
}